// stores/user.js
import { defineStore } from 'pinia'
import { useMenuStore } from './menu'
import router from '@/router'
interface UserInfo {
  token: string
  info: any
}

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null as UserInfo | null,
    // 记录上次路由，用于登录后跳转回之前的页面
    oldRoute: ''
  }),
  actions: {
    login(user) {
      this.userInfo = user
    },
    logout() {
      const menuStore = useMenuStore()
      this.userInfo = null
      this.oldRoute = router.currentRoute.value.path
      menuStore.setMenuList([])
      router.push({
        path: '/login'
      })
    }
  },
  persist: true
})
